import { Tile_Interface } from "./tile_interface";
import { Tile_Tool } from "./tile_tool";
import { Adapter } from "../renderer/adapter";
import { BitMath } from "../math/math";
import { Tile_Bank } from "./tile_bank";

export namespace Tile_Scale9
{

    export class Scale9 implements Tile_Interface.IDrawElement
    {
        public topleft: Tile_Interface.IDrawElement_WithSize | null = null;
        public top: Tile_Interface.IDrawElement_WithSize | null = null;
        public topright: Tile_Interface.IDrawElement_WithSize | null = null;
        public left: Tile_Interface.IDrawElement_WithSize | null = null;
        public center: Tile_Interface.IDrawElement_WithSize | null = null;
        public right: Tile_Interface.IDrawElement_WithSize | null = null;
        public bottomleft: Tile_Interface.IDrawElement_WithSize | null = null;
        public bottom: Tile_Interface.IDrawElement_WithSize | null = null;
        public bottomright: Tile_Interface.IDrawElement_WithSize | null = null;
        public static CreateSingleBorderScale9(bank: Tile_Bank.Bank, name: string): Scale9
        {
            let s9 = new Scale9();
            s9.topleft = bank.GetTileByName(name + "_lt");
            s9.top = bank.GetTileByName(name + "_t");
            s9.topright = bank.GetTileByName(name + "_rt");
            s9.left = bank.GetTileByName(name + "_l");
            s9.center = null;
            s9.right = bank.GetTileByName(name + "_r");
            s9.bottomleft = bank.GetTileByName(name + "lb");
            s9.bottom = bank.GetTileByName(name + "b");
            s9.bottomright = bank.GetTileByName(name + "rb");

            if (s9.topleft == null)
                s9.topleft = bank.CreateTile(name + "_lt", Tile_Tool.TileTool.GenOnePixelBorder(true, true, false, false));
            if (s9.top == null)
                s9.top = bank.CreateTile(name + "_t", Tile_Tool.TileTool.GenOnePixelBorder(false, true, false, false));
            if (s9.topright == null)
                s9.topright = bank.CreateTile(name + "_rt", Tile_Tool.TileTool.GenOnePixelBorder(false, true, true, false));

            if (s9.left == null)
                s9.left = bank.CreateTile(name + "_l", Tile_Tool.TileTool.GenOnePixelBorder(true, false, false, false));

            if (s9.right == null)
                s9.right = bank.CreateTile(name + "_r", Tile_Tool.TileTool.GenOnePixelBorder(false, false, true, false));

            if (s9.bottomleft == null)
                s9.bottomleft = bank.CreateTile(name + "_lb", Tile_Tool.TileTool.GenOnePixelBorder(true, false, false, true));
            if (s9.bottom == null)
                s9.bottom = bank.CreateTile(name + "_b", Tile_Tool.TileTool.GenOnePixelBorder(false, false, false, true));
            if (s9.bottomright == null)
                s9.bottomright = bank.CreateTile(name + "_rb", Tile_Tool.TileTool.GenOnePixelBorder(false, false, true, true));

            return s9;
        }
        public static CreateTwoBorderScale9(bank: Tile_Bank.Bank, name: string): Scale9
        {
            let s9 = new Scale9();
            s9.topleft = bank.GetTileByName(name + "_lt");
            s9.top = bank.GetTileByName(name + "_t");
            s9.topright = bank.GetTileByName(name + "_rt");
            s9.left = bank.GetTileByName(name + "_l");
            s9.center = null;
            s9.right = bank.GetTileByName(name + "_r");
            s9.bottomleft = bank.GetTileByName(name + "lb");
            s9.bottom = bank.GetTileByName(name + "b");
            s9.bottomright = bank.GetTileByName(name + "rb");

            if (s9.topleft == null)
                s9.topleft = bank.CreateTile(name + "_lt", Tile_Tool.TileTool.GenTwoPixelBorder(true, true, false, false));
            if (s9.top == null)
                s9.top = bank.CreateTile(name + "_t", Tile_Tool.TileTool.GenTwoPixelBorder(false, true, false, false));
            if (s9.topright == null)
                s9.topright = bank.CreateTile(name + "_rt", Tile_Tool.TileTool.GenTwoPixelBorder(false, true, true, false));

            if (s9.left == null)
                s9.left = bank.CreateTile(name + "_l", Tile_Tool.TileTool.GenTwoPixelBorder(true, false, false, false));

            if (s9.right == null)
                s9.right = bank.CreateTile(name + "_r", Tile_Tool.TileTool.GenTwoPixelBorder(false, false, true, false));

            if (s9.bottomleft == null)
                s9.bottomleft = bank.CreateTile(name + "_lb", Tile_Tool.TileTool.GenTwoPixelBorder(true, false, false, true));
            if (s9.bottom == null)
                s9.bottom = bank.CreateTile(name + "_b", Tile_Tool.TileTool.GenTwoPixelBorder(false, false, false, true));
            if (s9.bottomright == null)
                s9.bottomright = bank.CreateTile(name + "_rb", Tile_Tool.TileTool.GenTwoPixelBorder(false, false, true, true));

            return s9;
        }


        //public void FillDraw(IWindowRender render, int x, int y, int w, int h, byte colorReplace)
        DrawScale(render: Adapter.IRenderAdapter, dest: BitMath.Rect, multiColor: BitMath.Color8Bit_RGB): void
        {
            //太小了没法画
            if (this.topleft == undefined || this.top == undefined || this.topright == undefined)
                throw new Error("not allow null");
            if (this.left == undefined || this.right == undefined)
                throw new Error("not allow null");
            if (this.bottomleft == undefined || this.bottom == undefined || this.bottomright == undefined)
                throw new Error("not allow null");

            let midwidth = dest.w - this.topleft.width - this.bottomright.width;
            let midheight = dest.h - this.topleft.height - this.bottomright.height;
            if (midwidth < 0 || midheight < 0)
                return;
            let tilew = this.topleft.width;
            let tileh = this.topleft.height;
            let x1 = dest.x;
            let x2 = x1 + tilew;
            let x3 = x2 + midwidth;
            let y1 = dest.y;
            let y2 = y1 + tileh;
            let y3 = y2 + midheight;
            this.topleft.Draw(render, x1, y1, multiColor);
            this.top.DrawScale(render, new BitMath.Rect(x2, y1, midwidth, tileh), multiColor);//h scale
            this.topright.Draw(render, x3, y1, multiColor);

            this.left.DrawScale(render, new BitMath.Rect(x1, y2, tilew, midheight), multiColor);//v scale
            if (this.center != null)
            {
                this.center.DrawScale(render, new BitMath.Rect(x2, y2, midwidth, midheight), multiColor);//hv scale
            }
            this.right.DrawScale(render, new BitMath.Rect(x3, y2, tilew, midheight), multiColor);//v scale

            this.bottomleft.Draw(render, x1, y3, multiColor);
            this.bottom.DrawScale(render, new BitMath.Rect(x2, y3, midwidth, tileh), multiColor);//h scale
            this.bottomright.Draw(render, x3, y3, multiColor);
        }
    }
}
